package com.xinli.xinlifm.view.adapter.find;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.find.FindCircleBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz on 16-10-13.
 */

public class FindPagerAdapter extends PagerAdapter{
    private Context context;
    private List<FindCircleBean.DataBean> list;
    private Pools.Pool<ImageView> pool = new Pools.SimplePool<>(7);

    public FindPagerAdapter(Context context, List<FindCircleBean.DataBean> lists) {
        this.context = context;
        this.list = new ArrayList<>(lists);
        //最后一个补0 位置的，第一个不最后位置的
        this.list.add(0,lists.get(lists.size()-1));
        this.list.add(lists.get(0));
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = pool.acquire();
//        ImageView imageView =null;
        if (imageView == null) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
        FindCircleBean.DataBean dataBean = list.get(position);
        imageView.setOnClickListener(dataBean);

        Picasso.with(context).load(dataBean.getCover()).config(Bitmap.Config.RGB_565).placeholder(R.mipmap.default_3_2).into(imageView);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        pool.release((ImageView) object);
    }
}
